﻿using System;
using System.Collections.Generic;
using System.Web.SessionState; //第一步：导入此命名空间
//ashx文件要使用Session,必须实现Session接口;
using System.Linq;
using System.Web;
using System.IO;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.BLL.Tbl;
using HKEA.CMS.Core.BLL.App;

namespace WebApplication.ServerAshxFile
{
    /// <summary>
    /// M_Basicinformation 的摘要说明
    /// </summary>
    public class M_Basicinformation : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";//"text/plain";
            string type = context.Request.QueryString["type"];

            if (!string.IsNullOrEmpty(type))
            {
                switch (type)
                {
                    case "1"://获取一个MediaRoomRelated 的html
                        GetOneMediaRoomRelated(context);
                        break;
                    case "2"://获取一个MediaRoom的html
                        GetLastMediaRoom(context);
                        break;
                    default:
                        break;
                }
            }
        }

        public void GetOneMediaRoomRelated(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string ret_html = string.Empty;
            string mediaroomUID = context.Request.QueryString["mediaroomUID"];
            string customerUID =context.Request.QueryString["CustomerUID"];
            if (!string.IsNullOrEmpty(mediaroomUID))
            {
                tblMediaRoom entTbl = new tblMediaRoom();
                app_tblMediaRoom entApp = new app_tblMediaRoom();
                entApp.CustomerUID = Convert.ToInt32(customerUID);
                entApp.AppMediaRoomUID = Convert.ToInt32(mediaroomUID); 
                entApp.IsDeleted = Constant.IsDelete.no;
                entTbl = tblMediaRoomBLL.GetInstance().GetEntity(int.Parse(mediaroomUID));
                entApp = app_tblMediaRoomBLL.GetInstance().GetEntity(entApp);
                if (entTbl != null && entApp==null)
                {
                    string media_url = string.Format("{0}{1}", entTbl.FilePath, entTbl.FileName);
                    ret_html = string.Format("<li id='mediaroom_{0}'><p class='p2'><a href='{1}_600_400.jpg'><img src='{2}_100_100.jpg' alt='' /></a></p><div class='p4'><span class='showbig'>View</span> | <span class='closeBtnbig'><a href='javascript:delMediaRoomRelated({3})'>Delete</a></span></div></li>", entTbl.MediaRoomUID, media_url, media_url, entTbl.MediaRoomUID);
                }
                else if (entApp != null)
                {
                    string media_url = string.Format("{0}{1}", entApp.FilePath, entApp.FileName);
                    ret_html = string.Format("<li id='mediaroom_{0}'><p class='p2'><a href='{1}_600_400.jpg'><img src='{2}_100_100.jpg' alt='' /></a></p><div class='p4'><span class='showbig'>View</span> | <span class='closeBtnbig'><a href='javascript:delMediaRoomRelated({3})'>Delete</a></span></div></li>", entApp.AppMediaRoomUID, media_url, media_url, entApp.AppMediaRoomUID);
                }
            }
            context.Response.Write(ret_html);
        }

        public void GetLastMediaRoom(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string ret_html = string.Empty;
            string ret = string.Empty;
            //tblMediaRoom entTbl = new tblMediaRoom();
            //entTbl = GetTblLastMediaRoom();
            
            app_tblMediaRoom entApp = new app_tblMediaRoom();
            entApp = GetAppLastMediaRoom();
            if ( entApp!=null)
            {
                string media_url = string.Format("{0}{1}", entApp.FilePath, entApp.FileName);
                ret_html = string.Format("<li><img alt='{0}' src='{1}_50_50.jpg'></li>", entApp.AppMediaRoomUID, media_url);
                ret = entApp.AppMediaRoomUID + "&" + ret_html;
            }

            context.Response.Write(ret);
        }


        private tblMediaRoom GetTblLastMediaRoom()
        {
            tblMediaRoom ret = new tblMediaRoom();
            ret = tblMediaRoomBLL.GetInstance().GetLastEntity();
            return ret;
        }
        private app_tblMediaRoom GetAppLastMediaRoom()
        {
            app_tblMediaRoom ret = new app_tblMediaRoom();
            ret = app_tblMediaRoomBLL.GetInstance().GetLastEntity();
            return ret;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}